package com.winchain.chainbill.bill.biz.send.cmd;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.base.utils.DateUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.framework.command.CallbackContext;
import com.shell.framework.command.CommandContext;
import com.shell.framework.command.ICallback;
import com.shell.framework.command.ICommand;
import com.winchain.chainbill.bill.base.po.CbRcv;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.po.CbSnd;
import com.winchain.chainbill.bill.base.service.CbRcvService;
import com.winchain.chainbill.bill.biz.Bill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillRcv4SndCmd implements ICommand<Bill<CbSnd>>, ICallback<Bill<CbSnd>, AcctTxRec> {
	@Autowired
	private CbRcvService cbRcvService;

	public boolean execute(CommandContext<Bill<CbSnd>> context) throws Exception {
		CbRec record = ((Bill) context.getSource()).getRec();
		CbSnd send = (CbSnd) ((Bill) context.getSource()).getSource();

		CbRcv rcv = getRcv(record, send);
		this.cbRcvService.save(rcv);
		return false;
	}

	private CbRcv getRcv(CbRec record, CbSnd send) {
		CbRcv rcv = new CbRcv();
		rcv.setRcvId(SystemUtil.getUUID());
		rcv.setBillId(send.getBillId());
		rcv.setCreateTime(DateUtil.getSystemDateTime());
		rcv.setSellerId(send.getSellerId());
		rcv.setSellerNm(send.getSellerNm());
		rcv.setTxDate(send.getSndState());
		rcv.setBuyerId(send.getBuyerId());
		rcv.setBuyerNm(send.getBuyerNm());
		rcv.setRcvState("1");
		rcv.setOptSeq(record.getOptSeq());
		rcv.setRemark(send.getRemark());
		rcv.setTxType("0");
		rcv.setTxId(send.getSndId());
		rcv.setRcvLvl(Integer.valueOf(1));
		return rcv;
	}

	public boolean callback(CallbackContext<Bill<CbSnd>, AcctTxRec> context) throws Exception {
		execute(context);
		return false;
	}
}